﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.IDAL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class MasterPurchaseRuleMappingDAL : IDALMasterPurchaseRuleMapping
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasterPurchaseRuleMappingDAL()
        {

        }

        public IList<MasterPurchaseRuleMappingInfo> GetMasterPurchaseRuleMapping()
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseRuleMappingInfo> infoList = new List<MasterPurchaseRuleMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT rm.*"
                            + ", r.ICM_PC_RULE_NAME"
                            + ", s.BGM_SOURCE_NAME"
                            + ", s.BGM_SOURCE_TYPE_SEQ"
                            + ", st.BGM_SOURCE_TYPE_NAME"
                        + " FROM ICM_MAS_PURCHASE_RULE_MAPPING rm"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON rm.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE s ON rm.BGM_SOURCE_SEQ = s.BGM_SOURCE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE_TYPE st ON s.BGM_SOURCE_TYPE_SEQ = st.BGM_SOURCE_TYPE_SEQ"
                        + " WHERE rm.RECORD_STATUS <> @delete_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseRuleMappingInfo info = new MasterPurchaseRuleMappingInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        BgmSourceInfo infoSource = new BgmSourceInfo();
                        BgmSourceTypeInfo infoSourceType = new BgmSourceTypeInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_RULE_MAP_SEQ"]);
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        infoSource.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_SEQ"]));
                        infoSource.Name = (dr[i]["BGM_SOURCE_NAME"]).ToString();
                        infoSourceType.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_TYPE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_TYPE_SEQ"]));
                        infoSourceType.Name = (dr[i]["BGM_SOURCE_TYPE_NAME"]).ToString();
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        infoSource.InfoSourceType = infoSourceType;
                        info.InfoSource = infoSource;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterPurchaseRuleMappingInfo> GetMasterPurchaseRuleMapping(string record_status)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseRuleMappingInfo> infoList = new List<MasterPurchaseRuleMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@record_status", record_status);

                strSQL = "SELECT rm.*"
                            + ", r.ICM_PC_RULE_NAME"
                            + ", s.BGM_SOURCE_NAME"
                            + ", s.BGM_SOURCE_TYPE_SEQ"
                            + ", st.BGM_SOURCE_TYPE_NAME"
                        + " FROM ICM_MAS_PURCHASE_RULE_MAPPING rm"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON rm.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE s ON rm.BGM_SOURCE_SEQ = s.BGM_SOURCE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE_TYPE st ON s.BGM_SOURCE_TYPE_SEQ = st.BGM_SOURCE_TYPE_SEQ"
                        + " WHERE rm.RECORD_STATUS = @record_status";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseRuleMappingInfo info = new MasterPurchaseRuleMappingInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        BgmSourceInfo infoSource = new BgmSourceInfo();
                        BgmSourceTypeInfo infoSourceType = new BgmSourceTypeInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_RULE_MAP_SEQ"]);
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        infoSource.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_SEQ"]));
                        infoSource.Name = (dr[i]["BGM_SOURCE_NAME"]).ToString();
                        infoSourceType.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_TYPE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_TYPE_SEQ"]));
                        infoSourceType.Name = (dr[i]["BGM_SOURCE_TYPE_NAME"]).ToString();
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        infoSource.InfoSourceType = infoSourceType;
                        info.InfoSource = infoSource;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<MasterPurchaseRuleMappingInfo> GetMasterPurchaseRuleMappingWithRule(int pc_rule_seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<MasterPurchaseRuleMappingInfo> infoList = new List<MasterPurchaseRuleMappingInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@pc_rule_seq", pc_rule_seq);

                strSQL = "SELECT rm.*"
                            + ", r.ICM_PC_RULE_NAME"
                            + ", s.BGM_SOURCE_NAME"
                            + ", s.BGM_SOURCE_TYPE_SEQ"
                            + ", st.BGM_SOURCE_TYPE_NAME"
                        + " FROM ICM_MAS_PURCHASE_RULE_MAPPING rm"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON rm.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE s ON rm.BGM_SOURCE_SEQ = s.BGM_SOURCE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE_TYPE st ON s.BGM_SOURCE_TYPE_SEQ = st.BGM_SOURCE_TYPE_SEQ"
                        + " WHERE rm.ICM_PC_RULE_SEQ = @pc_rule_seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        MasterPurchaseRuleMappingInfo info = new MasterPurchaseRuleMappingInfo();
                        MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                        BgmSourceInfo infoSource = new BgmSourceInfo();
                        BgmSourceTypeInfo infoSourceType = new BgmSourceTypeInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                        info.Seq = Convert.ToInt32(dr[i]["ICM_PC_RULE_MAP_SEQ"]);
                        infoRule.Seq = (Convert.IsDBNull(dr[i]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_PC_RULE_SEQ"]));
                        infoRule.Name = (dr[i]["ICM_PC_RULE_NAME"]).ToString();
                        infoSource.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_SEQ"]));
                        infoSource.Name = (dr[i]["BGM_SOURCE_NAME"]).ToString();
                        infoSourceType.Seq = (Convert.IsDBNull(dr[i]["BGM_SOURCE_TYPE_SEQ"]) ? null : (Nullable<int>)(dr[i]["BGM_SOURCE_TYPE_SEQ"]));
                        infoSourceType.Name = (dr[i]["BGM_SOURCE_TYPE_NAME"]).ToString();
                        infoRecord.Created_by = (Convert.IsDBNull(dr[i]["CREATED_BY"]) ? null : (Nullable<int>)(dr[i]["CREATED_BY"]));
                        infoRecord.Created_when = (Convert.IsDBNull(dr[i]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["CREATED_WHEN"]));
                        infoRecord.Updated_by = (Convert.IsDBNull(dr[i]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[i]["UPDATED_BY"]));
                        infoRecord.Updated_when = (Convert.IsDBNull(dr[i]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["UPDATED_WHEN"]));
                        infoRecord.Restored_by = (Convert.IsDBNull(dr[i]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[i]["RESTORED_BY"]));
                        infoRecord.Restored_when = (Convert.IsDBNull(dr[i]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[i]["RESTORED_WHEN"]));
                        infoStatus.Status = (dr[i]["RECORD_STATUS"]).ToString();
                        infoRecord.InfoStatus = infoStatus;
                        infoSource.InfoSourceType = infoSourceType;
                        info.InfoSource = infoSource;
                        info.InfoRule = infoRule;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public MasterPurchaseRuleMappingInfo GetMasterPurchaseRuleMapping(int seq)
        {
            string strSQL;
            DataSet ds;
            DataRowCollection dr;
            MasterPurchaseRuleMappingInfo info = new MasterPurchaseRuleMappingInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT rm.*"
                            + ", r.ICM_PC_RULE_NAME"
                            + ", s.BGM_SOURCE_NAME"
                            + ", s.BGM_SOURCE_TYPE_SEQ"
                            + ", st.BGM_SOURCE_TYPE_NAME"
                        + " FROM ICM_MAS_PURCHASE_RULE_MAPPING rm"
                            + " INNER JOIN ICM_MAS_PURCHASE_RULE r ON rm.ICM_PC_RULE_SEQ = r.ICM_PC_RULE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE s ON rm.BGM_SOURCE_SEQ = s.BGM_SOURCE_SEQ"
                            + " INNER JOIN BGM_MAS_SOURCE_TYPE st ON s.BGM_SOURCE_TYPE_SEQ = st.BGM_SOURCE_TYPE_SEQ"
                        + " WHERE rm.ICM_PC_RULE_MAP_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    MasterPurchaseRuleInfo infoRule = new MasterPurchaseRuleInfo();
                    BgmSourceInfo infoSource = new BgmSourceInfo();
                    BgmSourceTypeInfo infoSourceType = new BgmSourceTypeInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();
                    info.Seq = Convert.ToInt32(dr[0]["ICM_PC_RULE_MAP_SEQ"]);
                    infoRule.Seq = (Convert.IsDBNull(dr[0]["ICM_PC_RULE_SEQ"]) ? null : (Nullable<int>)(dr[0]["ICM_PC_RULE_SEQ"]));
                    infoRule.Name = (dr[0]["ICM_PC_RULE_NAME"]).ToString();
                    infoSource.Seq = (Convert.IsDBNull(dr[0]["BGM_SOURCE_SEQ"]) ? null : (Nullable<int>)(dr[0]["BGM_SOURCE_SEQ"]));
                    infoSource.Name = (dr[0]["BGM_SOURCE_NAME"]).ToString();
                    infoSourceType.Seq = (Convert.IsDBNull(dr[0]["BGM_SOURCE_TYPE_SEQ"]) ? null : (Nullable<int>)(dr[0]["BGM_SOURCE_TYPE_SEQ"]));
                    infoSourceType.Name = (dr[0]["BGM_SOURCE_TYPE_NAME"]).ToString();
                    infoRecord.Created_by = (Convert.IsDBNull(dr[0]["CREATED_BY"]) ? null : (Nullable<int>)(dr[0]["CREATED_BY"]));
                    infoRecord.Created_when = (Convert.IsDBNull(dr[0]["CREATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["CREATED_WHEN"]));
                    infoRecord.Updated_by = (Convert.IsDBNull(dr[0]["UPDATED_BY"]) ? null : (Nullable<int>)(dr[0]["UPDATED_BY"]));
                    infoRecord.Updated_when = (Convert.IsDBNull(dr[0]["UPDATED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["UPDATED_WHEN"]));
                    infoRecord.Restored_by = (Convert.IsDBNull(dr[0]["RESTORED_BY"]) ? null : (Nullable<int>)(dr[0]["RESTORED_BY"]));
                    infoRecord.Restored_when = (Convert.IsDBNull(dr[0]["RESTORED_WHEN"]) ? null : (Nullable<DateTime>)(dr[0]["RESTORED_WHEN"]));
                    infoStatus.Status = (dr[0]["RECORD_STATUS"]).ToString();
                    infoRecord.InfoStatus = infoStatus;
                    infoSource.InfoSourceType = infoSourceType;
                    info.InfoSource = infoSource;
                    info.InfoRule = infoRule;
                    info.InfoRecord = infoRecord;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(IList<MasterPurchaseRuleMappingInfo> infoList, int rule_seq, IDBManager dbManagerTrans)
        {
            string strSQL;
            try
            {
                for (int i = 0; i < infoList.Count; i++)
                {
                    dbManagerTrans.CreateParameters(5);

                    dbManagerTrans.AddParameters(0, "@pc_rule_seq", rule_seq);
                    dbManagerTrans.AddParameters(1, "@source_seq", infoList[i].InfoSource.Seq);
                    dbManagerTrans.AddParameters(2, "@record_status", SystemInfo._WorkRecordStatus);
                    dbManagerTrans.AddParameters(3, "@created_by", infoList[i].InfoRecord.Created_by);
                    dbManagerTrans.AddParameters(4, "@created_when", infoList[i].InfoRecord.Created_when);

                    strSQL = "INSERT INTO ICM_MAS_PURCHASE_RULE_MAPPING"
                        + " (ICM_PC_RULE_SEQ, BGM_SOURCE_SEQ"
                        + " , RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                        + " VALUES(@pc_rule_seq"
                        + ", @source_seq"
                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when"
                        + " )";

                    dbManagerTrans.ExecuteNonQuery(CommandType.Text, strSQL);
                }
            }
            catch
            {
                throw;
            }
        }

        public void Update(MasterPurchaseRuleMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@pc_rule_seq", info.InfoRule.Seq);
                dbManager.AddParameters(2, "@source_seq", info.InfoSource.Seq);
                dbManager.AddParameters(3, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(4, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(5, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " SET ICM_PC_RULE_SEQ = @pc_rule_seq"
                    + ", BGM_SOURCE_SEQ = @source_seq"
                    + ", RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_RULE_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasterPurchaseRuleMappingInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.InfoRecord.Updated_by);
                dbManager.AddParameters(3, "@updated_when", info.InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_RULE_MAP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteAll(IList<MasterPurchaseRuleMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", infoList[0].InfoRecord.Updated_by);
                dbManager.AddParameters(2, "@updated_when", infoList[0].InfoRecord.Updated_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", UPDATED_BY = @updated_by"
                    + ", UPDATED_WHEN = @updated_when"
                    + " WHERE ICM_PC_RULE_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(IList<MasterPurchaseRuleMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();

                strSQL = "DELETE FROM ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " WHERE ICM_PC_RULE_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteFromDB(Nullable<int> rule_seq, IDBManager dbManagerTrans)
        {
            string strSQL;

            try
            {
                dbManagerTrans.CreateParameters(1);

                dbManagerTrans.AddParameters(0, "@rule_seq", rule_seq);

                strSQL = "DELETE FROM ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " WHERE ICM_PC_RULE_SEQ = @rule_seq";

                dbManagerTrans.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
        }

        public void Restore(IList<MasterPurchaseRuleMappingInfo> infoList)
        {
            string strSQL;
            string seq = "";
            for (int i = 0; i < infoList.Count; i++)
            {
                if (i == 0)
                {
                    seq += infoList[i].Seq;
                }
                else
                {
                    seq += "," + infoList[i].Seq;
                }
            }

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(1, "@restored_by", infoList[0].InfoRecord.Restored_by);
                dbManager.AddParameters(2, "@restored_when", infoList[0].InfoRecord.Restored_when);

                strSQL = "UPDATE ICM_MAS_PURCHASE_RULE_MAPPING"
                    + " SET RECORD_STATUS = @record_status"
                    + ", RESTORED_BY = @restored_by"
                    + ", RESTORED_WHEN = @restored_when"
                    + " WHERE ICM_PC_RULE_MAP_SEQ IN (" + seq + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
